package com.xdwanj.service.impl;

import com.xdwanj.dao.UserDao;
import com.xdwanj.dao.impl.UserDaoImpl;
import com.xdwanj.pojo.Pet;
import com.xdwanj.pojo.User;
import com.xdwanj.service.UserService;
import com.xdwanj.util.PageBean;

public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();

    @Override //TODO
    public User login(String username, String password) {
        User user = null;
        try {
            user = userDao.findUserByUserNameAndPassword(username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return user;
    }

    @Override
    public User register(User user) {
        User userReturn = null;

        try {
            userDao.addUser(user);
            userReturn = userDao.findUserByUserNameAndPassword(user.getUsername(), user.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return userReturn;
    }

    @Override
    public boolean addUser(User user) {
        try {
            userDao.addUserAll(user);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override //TODO
    public boolean updatePassword(int id, String NewPassword) {
        try {
            userDao.updatePassWord(id, NewPassword);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean updateUser(User user) {
        try {
            userDao.updateUser(user);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteUser(int id) {
        try {
            userDao.deleteUser(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public PageBean<User> findUser(int pageSize, int currentPageNo) {
        PageBean<User> customerPageDto = null;
        try {
            customerPageDto = userDao.findAll(pageSize, currentPageNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customerPageDto;
    }

    @Override //TODO
    public PageBean<Pet> findPetListByUid(int id, int pageSize, int currentPageNo) {
        PageBean<Pet> customerPageDto = null;
        try {
            customerPageDto = userDao.findPetListById(id, pageSize, currentPageNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customerPageDto;
    }

    @Override
    public PageBean<User> findUserByName(String name, int pageSize, int currentPageNo) {
        PageBean<User> users = null;
        try {
            users = userDao.findUserListByName(name, pageSize, currentPageNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return users;
    }


}
